package com.service;
import com.dao.AsLogsDao;
import com.entity.AsLogs;
import com.utils.JsonUtil;
import com.utils.Pager;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;

@Service
public class AsLogsService
{
    @Resource
    private AsLogsDao asLogsDao;

    //向日志表中写入一条记录
    public Integer addLog( Integer userId , String userName , String operateInfo )
    {
        AsLogs log = new AsLogs();
        log.setUserId( userId );
        log.setUserName( userName );
        String str = "用户[" + userName + "]进行[" + operateInfo +"]操作";
        log.setOperateInfo( str );
        log.setOperateDatetime( new Date() );
        return asLogsDao.addOneLog( log );
    }

    //查询指定用户的操作日志数量
    public String returnLogsPageByUserId( Integer userId , Date operateDatetime , Integer page , Integer size )
    {
        Integer total = asLogsDao.getLogsCountByUserId( userId , operateDatetime );
        Integer index = ( page - 1 ) * size;
        ArrayList<AsLogs> logsList = asLogsDao.getLogsPageByUserId( userId , operateDatetime , index , size );
        Pager<AsLogs> pager = new Pager<>();
        pager.setTotal( total );
        pager.setRows( logsList );
        String jsonStr = JsonUtil.toJsonStrWithDateTime( pager );

        return jsonStr;
    }
}
